// 引入此文件需自建如下内容：
// footerButtons
import { transferDataDetail } from '@/components/task/utils';
import { PLAN_TYPE } from '@/constants';
import { mapGetters } from 'vuex';

export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    detail: {
      type: Object,
      default: () => ({}),
    },
    planId: {
      type: [String, Number],
      default: '',
    },
    planType: {
      type: String,
      default: 'BP',
    },
    currentNode: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    this.PLAN_TYPE = PLAN_TYPE;
    return {
      detailStartCopy: [],
      detailEndCopy: [],
    };
  },
  computed: {
    ...mapGetters(['spinning']),

    titleName() {
      const type = `${PLAN_TYPE[this.planType]}计划`;
      const node = this.currentNode.label;
      const status =
        this.hasEdit && this.currentNode.key !== 'PlanStart'
          ? '待办'
          : undefined;
      return [type, node, status].filter(item => item).join('-');
    },
    processId() {
      return this.detail?.plan?.processId;
    },
    hasEdit() {
      return this.detail.isCurrentProcessor === false ? false : true;
    },
    filterButtons() {
      if (this.hasEdit) return this.footerButtons;
      return this.footerButtons.filter(item => !item.auth);
    },
  },
  watch: {
    visible: {
      handler(val) {
        if (!val) return;
        const plan = this.detail?.plan || {};
        const timeRanges = [plan.startTime, plan.endTime];
        console.log('timeRanges: ', timeRanges);
        this.form = {
          salesOrganization: plan.salesOrganization || '',
          serialNumber: plan.serialNumber || '',
          name: plan.name || '',
          startTime: timeRanges,
          deadlineTime: plan.deadlineTime || null,
        };

        // 初版明细
        const planDetailList = this.detail?.planDetailList || [];
        this.detailStartCopy = JSON.parse(JSON.stringify(planDetailList));
        this.planDetailList = transferDataDetail(planDetailList, timeRanges);

        // 终版明细
        const endPlanDetailList = this.detail?.endPlanDetailList || [];
        this.detailEndCopy = JSON.parse(JSON.stringify(endPlanDetailList));

        if (!endPlanDetailList.length) return;
        this.endPlanDetailList = transferDataDetail(
          endPlanDetailList,
          timeRanges
        );
      },
      immediate: true,
    },
  },
  methods: {},
};
